var sql = require('../Resource/db.sql');
var config = require('../config.js');
var dbName = 'mydb';
var peopleCount = config.bigBonus.count + config.perfectBonus.count + config.happyBonus.count + config.notBonus.count;
module.exports = {
	db: null,
	instance(){
		if(!this.db) this.db = global.openDatabase(dbName, '1.0', 'my first database', 2 * 1024 * 1024);
		return this.db;
	},
	initSql(tx, sql){
		tx.executeSql('CREATE TABLE IF NOT EXISTS member (id unique, name, join_date, month, level)');
		tx.executeSql('DELETE FROM member');
		sql.split(';').map((l)=>{
			tx.executeSql(l, [], function(){}, function(a, e){console.log(l, e);});
		});
		tx.executeSql('UPDATE member SET level = 2 WHERE month >= 12');
		tx.executeSql('UPDATE member SET level = 1 WHERE id in ("015", "048") OR month < 12'); // 去除总经理、副总经理和未满一年以上的员工
		tx.executeSql('UPDATE member SET level = 0 WHERE month < 2');
	},
	initDB(){
		var self = this;
		return new Promise((resolve, reject)=> {
			console.log("初始化数据库");
			self.instance().transaction(tx=>{
				tx.executeSql('SELECT * FROM member', [], (tx, result)=>{
					if (result.rows.length !== peopleCount) {
						self.initSql(tx, sql);
					}
				}, (tx, error)=>{
					self.initSql(tx, sql);
				});
				resolve();
			});
		});
	},
	/**
	 * 请求sql
	 * DB.query();
	 */
	query(sql, param){
		if (param instanceof Function) {
			handler = param;
			param = [];
		}
		return new Promise(((resolve, reject)=> {
			this.instance().transaction(function (tx) {
				tx.executeSql(sql, param, function(tx, result){
					var data = [];
					for(var i=0; i<result.rows.length; i++) {
						data[i] = result.rows.item(i);
					}
					resolve(data, tx);
				}, function(tx, error){
					reject(error, tx);
				});
			});
		}).bind(this));
	}
}